/**
 * tv视频详情页面
 * @author luobiao
*/
$(function(){
	show_detail.run();	
});

var show_detail = {	
		run: function(){
			this.__init__();			
			this._start_();
			this._bind_events();
		},
		
		__init__: function(){
			this.contextPath = main.contextPath;
			this.showId = $("#showId").val();
			this.itemUrl = $("#itemUrl").val();
			this.title = $("#title").val();
			this.vdInstalled = vd.check();
			side_pub._init_side_genre_decade(this);
			
		},
		
		_start_: function(){			
			share.run();
			this._get_episodes_detail();
		},
		
		_bind_events: function(){
			$("#genre_div > div.vscroll-content > ul > li > a").click(function(){
				show_detail.genre = $(this).attr("genre");
				show_detail._genre_decade_relocation_event();
				return false;
			});			
			$("#decade_div > ul > li > a").click(function(){
				var decadeTemp = $(this).text();
				show_detail.decade = decadeTemp.substring(0,4);
				show_detail._genre_decade_relocation_event();
				return false;
			});
			
			$("#m_info_cont > ul > li > a.btn_play2").click(function(){
				var href = $(this).attr("href");
				var queryGenre = show_detail.genre;
				var queryDecade = show_detail.decade;
				var data = [];
				if(queryGenre){
					data.push("genre=" + queryGenre);
				}
				if(queryDecade){
					data.push("decade=" + queryDecade);
				}
				_gaq.push(['_trackEvent', 'TVShow', 'Play', href]);
				
				location.href = href + "#" + data.join("&");
				return false;
			});
			
		},
		
		_genre_decade_relocation_event: function(){
			var genre = show_detail.genre;
			var decade = show_detail.decade;
			var params = [];
			if(genre){
				params.push("genre=" + genre);
			}
			if(decade){
				params.push("decade=" + decade);
			}
			if(!genre && !decade){
				location.href = main.contextPath + "/tv/full-length/";
				return false;
			}
			location.href = main.contextPath + "/tv/full-lengths#" + params.join("&");
		},
		
		_get_episodes_detail: function(){
			var url = this.contextPath + "/tv/full-length/episodes_detail?showId=" + this.showId;
			$.get(url, function(result){
				$("#show_episodes_div").html(result);
				show_detail.__bind_season_change_events();
				$("#show_episodes_div > div > div.tv_eCont > table > tbody > tr > td > a.btn_p3").click(function(){
					var href = $(this).attr("href");
					var queryGenre = show_detail.genre;
					var queryDecade = show_detail.decade;
					var data = [];
					if(queryGenre){
						data.push("genre=" + queryGenre);
					}
					if(queryDecade){
						data.push("decade=" + queryDecade);
					}
					_gaq.push(['_trackEvent', 'TVShow', 'Play', href]);
					location.href = href + "#" + data.join("&");
					return false;
				});
				
				$("#show_episodes_div > div > div.tv_eCont > table > tbody > tr > td > a.btn_d3").click(function(){
					var url = $(this).attr("href");
					var title = $(this).attr("title");
					
					if (!this.vdInstalled) {
						show_detail.$alertDialog.dialog({
							submit : function() {
								vd.keep();
								_gaq.push([ '_trackEvent', 'TVShow', 'Download', url ]);
								vd.go(url, title);
								show_detail.vdInstalled = true;
							}
						});
					}
					
					if (!show_detail.vdInstalled) {
						show_detail.$alertDialog.dialog('open');
					} else {
						_gaq.push([ '_trackEvent', 'TVShow', 'Download', url ]);
						vd.go(url, title);
					}
					return false;
					/*if( ! show_detail.vdInstalled){
						show_detail.itemUrl = url;
						show_detail.title = title;
						show_detail.$alertDialog.dialog('open');								
					}else{
						_gaq.push(['_trackEvent', 'TVShow', 'Download', url]);
						vd.go(url, title);					
					}*/						
				});
				
			});
		},
		
		//不同season切换
		__bind_season_change_events: function(){
			$("div.tv_sMenu > ul > li > a ").live('click',function(){
				var divId = $(this).attr('divId');
				$(".displayorhide").css("display","none");
				$("#"+divId).css("display","block");
			});
		}
};

var share = {
	run: function(){
		//share btn
		$("#share_input").val(location.href);
		
		$("#fb_share_a").click(function(){
			var url = $("#share_input").val();
			var fbUrl = "https://www.facebook.com/sharer.php?u=" + encodeURIComponent(url);
			_gaq.push(['_trackEvent', 'TVShow', 'fb_share', url]);	
			window.open(fbUrl);
		});
		
		$("#tw_share_a").click(function(){
			var url = $("#share_input").val();
			var twUrl = "https://twitter.com/share?url=" +encodeURIComponent(url);
			_gaq.push(['_trackEvent', 'TVShow', 'twitter_share', url]);				
			window.open(twUrl);
		});
	}
};